package com.sky.service.impl;

import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopcartMapper;
import com.sky.service.ShopcartService;
import com.sky.vo.SetmealVO;
import lombok.extern.apachecommons.CommonsLog;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShopcartServiceImpl implements ShopcartService {
    @Autowired
    private ShopcartMapper shopcartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void save(ShoppingCartDTO shoppingCartDTO) {
        //将数据存储到ShoppingCart对象中
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        log.info("将数据存储在shoppingCart对象中：{}", shoppingCart);

        // 获取当前用户id
        BaseContext.getCurrentId();

        // 查询当前菜品或套餐是否在购物车中
        List<ShoppingCart> list = shopcartMapper.list(shoppingCart);

        // 若存在，则更新数量+1，否则，插入数据
        if (list != null && list.size() > 0){
            //数量加一
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shopcartMapper.update(cart);
        }else {
            //插入数据
            //查看插入的是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null){
                //插入菜品
                Dish dish = dishMapper.getDishById(shoppingCartDTO.getDishId());
                shoppingCart = ShoppingCart.builder()
                        .name(dish.getName())
                        .userId(BaseContext.getCurrentId())
                        .image(dish.getImage())
                        .dishId(dish.getId())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .amount(dish.getPrice())
                        .build();
            }else{
                //插入套餐
                SetmealVO setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart = ShoppingCart.builder()
                        .userId(BaseContext.getCurrentId())
                        .setmealId(setmeal.getId())
                        .name(setmeal.getName())
                        .image(setmeal.getImage())
                        .amount(setmeal.getPrice())
                        .build();
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shopcartMapper.insert(shoppingCart);
        }


    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart =new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //查看是否有该商品
        List<ShoppingCart> list = shopcartMapper.list(shoppingCart);
        if (list == null || list.size() == 0) { throw new DeletionNotAllowedException(MessageConstant.SHOPPING_CART_IS_NULL);}
        //查看该商品数量
        if (list.get(0).getNumber() == 1) {
            //数量为1，删除该商品
            shoppingCart=list.get(0);
            shopcartMapper.clean(shoppingCart);
        }else{
            //数量大于1，数量减1
            shoppingCart=list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shopcartMapper.update(shoppingCart);
        }
    }

    @Override
    public void clean() {
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shopcartMapper.clean(shoppingCart);
    }

    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shopcartMapper.list(shoppingCart);
        return list;

    }
}
